# import pymysql
#
# from config import TEL
#
#
# class DbTools:
#
#     # 创建连接
#     @staticmethod
#     def __creat_conn():
#         conn = pymysql.connect(host="211.103.136.244",
#                                port=7061, user="studert", password="iHRM_student_2022",
#                                database="ihrm", charset="utf8")
#         return conn
#
#     # 查一条
#     @staticmethod
#     def select_one(sql):
#         conn = None
#         cursor = None
#         ret = None
#         try:
#             conn = DbTools.__creat_conn()
#             cursor = conn.cursor()
#             cursor.execute(sql)
#             ret = cursor.fetchone()
#         except Exception as err:
#             print(err)
#         finally:
#             cursor.close()
#             conn.close()
#             return ret
#
#     def select_many(self):
#         pass
#
#     @staticmethod
#     def iud(sql):
#         conn = None
#         cursor = None
#         try:
#             conn = DbTools.__creat_conn()
#             cursor = conn.cursor()
#             ret2 = cursor.execute(sql)
#             conn.commit()
#             print("修改完成 Affacted rows:", ret2)
#         except Exception as err:
#             print(err)
#             conn.rollback()
#         finally:
#             cursor.close()
#             conn.close()
# if __name__ == '__main__':
#
#
#     # a = DbTools.select_one('select * from t_book; ')
#     # print(a)
#     a = DbTools.iud(f"delete from bs_user where mobile = '{TEL}';")
#     print(a)

import pymysql

class DBTools:
    def conn_db(self):
        conn = pymysql.connect(
            host='211.103.136.244',
            port=7061,
            user='student',
            password='iHRM_student_2022',
            database='ihrm',
            charset='utf8'
        )
        return conn
    def select_one(self, sql):
        cursor = None
        conn = None
        result = None
        try:
            conn = self.conn_db()
            cursor = conn.cursor()
            cursor.execute(sql)
            result = cursor.fetchone()
        except Exception as err:
            print('查询一条错误信息为：',err)
        finally:
            cursor.close()
            conn.close()
            return result

    def iud(self, sql):
        cursor = None
        conn = None
        try:
            conn = self.conn_db()
            cursor = conn.cursor()
            ret = cursor.execute(sql)
            conn.commit()
            print('增删改成功Affect rows：', ret)
        except Exception as err:
            conn.rollback()
            print('查询一条错误信息为：', err)
        finally:
            cursor.close()
            conn.close()


if __name__ == '__main__':
    iud = DBTools()
    iud.iud("insert into t_book(`read`,comment,pub_date,title) values (50,0,'2020-01-01','《Python从入门到放弃》');")
    iud.iud("update t_book set comment = 250 where title = '《Python从入门到放弃》';")
    iud.iud("delete from t_book where title = '《Python从入门到放弃》';")

    sel = DBTools()
    ret = sel.select_one("select * from t_book where '《Python从入门到放弃》';")
    print(ret)
